package com.example.androidtest1.proj.util;

import java.util.Collections;
import java.util.List;
import java.util.Vector;

public class RandSearcherImp implements Searcher {
    public static final int MAX_COUNT = 5040;


    public static void main(String[] args) {
        System.out.println(new RandSearcherImp().search(new int[]{1, 5, 5, 5}));
        System.out.println(new RandSearcherImp().search(new int[]{1, 4, 2, 3}));
        System.out.println(new RandSearcherImp().search(new int[]{1, 1, 1, 1}));
    }


    /**
     * 随机搜索器的实现v1.0
     * 枚举运算符的种类,构造出后缀表达式
     *
     * @param arr
     * @return
     */
    public List<String> search(int[] arr) {
        for (int x = 0; x < 4; x++)
            for (int y = 0; y < 4; y++)
                for (int z = 0; z < 4; z++) {
                    List<String> exp = new Vector<>();
                    exp.add(OPERATORS[x]);
                    exp.add(OPERATORS[y]);
                    exp.add(OPERATORS[z]);
                    for (int e : arr) exp.add(Integer.toString(e));
                    for (int i = 0; i < MAX_COUNT; i++) {
                        Collections.shuffle(exp);
                        if (Math.abs(PostExpression.eval(exp) - TARGET) < EPS) {
                            return exp;
                        }
                    }
                }
        return null;
    }

}
